import {isEmpty, sortBy, difference, compact, cloneDeep, isArray} from 'lodash'
import menuConfig from '../config/menu'
import {getGlobalData, getLocalData} from './globalScope'
import {browserHistory, hashHistory} from 'dva/router'
import Cookie from 'js-cookie'
import md5 from 'js-md5'
import QRCode from 'qrcode'
import JsBarcode from 'jsbarcode'

//获取url的参数
export function getParams(url = '') {
  let urlSplit = url.split('?'),
    args = {}; // 保存参数数据的对象
  if (isEmpty(urlSplit) || urlSplit.length === 1) {
    return args;
  }
  // 获取url中"?"符后的字串
  let qs = urlSplit[1].replace(/#\/$/, ''),
    items = qs.length ? qs.split("&") : [], // 取得每一个参数项,
    item = null,
    len = items.length;

  for (let i = 0; i < len; i++) {
    item = items[i].split("=");
    let name = decodeURIComponent(item[0]),
      value = decodeURIComponent(item[1]);
    if (name) {
      args[name] = value;
    }
  }
  return args;
}

//获取url的参数值
export function getParamValues(url = '') {
  let urlSplit = url.split('?'),
    args = []; // 保存参数数据的对象
  if (isEmpty(urlSplit) || urlSplit.length === 1) {
    return args;
  }
  // 获取url中"?"符后的字串
  let qs = urlSplit[1].replace(/#\/$/, ''),

    items = qs.length ? qs.split("&") : [], // 取得每一个参数项,
    item = null,
    len = items.length;

  for (let i = 0; i < len; i++) {
    item = items[i].split("=");
    let value = decodeURIComponent(item[1]);
    args.push(value)
  }
  return args;
}

//不同权限角色登录的默认页
export function setPathname(permissions) {
  function findKey(value) {
    const key = difference(sortList, list).find(item => {
      return item.startsWith(value)
    })
    list.push(key);
    if (key) {
      findKey(key);
    }
  }

  let list = [], pathname = '/', obj;
  const sortList = sortBy(permissions);
  list.push(sortList[0]);
  findKey(sortList[0]);
  list = compact(list);
  for (let i = 0; i < list.length; i++) {
    if (i === 0) {
      obj = menuConfig.find(item => item.value === list[i]);
      pathname = `${pathname}${obj.key}`;
    } else if (!isEmpty(obj.child)) {
      obj = obj.child.find(item => item.value === list[i]);
      pathname = `${pathname}/${obj.key}`;
    }
  }
  return pathname;
}

//校验直接输入的地址是否超出权限
export function checkAuth() {
  let path = null, obj = {}, result = '';
  let allAuthPaths = getGlobalData('authPaths');
  let dataSourceId = getGlobalData('dataSourceId');
  let authPaths = allAuthPaths ? allAuthPaths[dataSourceId] : []

  if (ENV === 'development') {
    path = location.pathname;
  } else {
    path = location.hash.slice(2, location.hash.indexOf('?'));
  }
  path = compact(path.split('/'));
  for (let i = 0; i < path.length; i++) {
    if (i === 0) {
      obj = menuConfig.find(item => item.key === path[i]);
      result = authPaths.find(item => item === obj.value);
    } else if (obj && !isEmpty(obj.child)) {
      obj = obj.child.find(item => item.key === path[i]);
      result = authPaths.find(item => item === obj.value);
    }
    if (isEmpty(result)) {
      if (ENV === 'development') {
        browserHistory.push('/');
      } else {
        hashHistory.push('/');
      }
    }
  }
}

//清除所有cookie
export function clearAllCookie() {
  let cookies = Cookie.get();
  Object.keys(cookies).forEach((key) => Cookie.remove(key));
}

//生成uuid
export function generateUuid() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  var uuid = s.join("");
  return uuid;
}

//加密
export function encrypt(str) {
  //固定加密串
  let pwd = 'oottbb';
  if (pwd == null || pwd.length <= 0) {
    return null;
  }
  var prand = "";
  for (var i = 0; i < pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
  var incr = Math.ceil(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  if (mult < 2) {
    return null;
  }
  var salt = Math.round(Math.random() * 1000000000) % 100000000;
  prand += salt;
  while (prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for (var i = 0; i < str.length; i++) {
    enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
    if (enc_chr < 16) {
      enc_str += "0" + enc_chr.toString(16);
    } else enc_str += enc_chr.toString(16);
    prand = (mult * prand + incr) % modu;
  }
  salt = salt.toString(16);
  while (salt.length < 8)salt = "0" + salt;
  enc_str += salt;
  return enc_str;
}
//解密
export function decrypt(str) {
  //固定加密串
  let pwd = 'oottbb';
  if (str == null || str.length < 8) {
    return '';
  }
  if (pwd == null || pwd.length <= 0) {
    return '';
  }
  var prand = "";
  for (var i = 0; i < pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
  var incr = Math.round(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  var salt = parseInt(str.substring(str.length - 8, str.length), 16);
  str = str.substring(0, str.length - 8);
  prand += salt;
  while (prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for (var i = 0; i < str.length; i += 2) {
    enc_chr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
    enc_str += String.fromCharCode(enc_chr);
    prand = (mult * prand + incr) % modu;
  }
  return enc_str;
}


//将[{id: myid, name: myname, pId: mypid }] 转为树形结构 [{id: myid, name: myname, children: [] }]
export function convertToTree(data, parentKey, firstLevelId = '0') {
  // 删除 所有 children,以防止多次调用
  data.forEach(item => {
    delete item.children;
  });
  // 将数据存储为 以Id为 KEY 的 map 索引数据列
  let map = {};
  data.forEach(item => {
    map[item.id] = item;
  });
  let list = [], result = [];
  data.forEach(item => {
    // 以当前遍历项的parentCategoryId,去map对象中找到索引的id
    let parent = map[item[parentKey]];
    // 如果找到索引，那么说明此项不在顶级当中,那么需要把此项添加到，他对应的父级中
    if (parent) {
      (parent.children || (parent.children = [])).push(item);
    } else {
      //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到list中，作为顶级
      list.push(item);
    }
  });
  list.forEach(item => (item[parentKey] === firstLevelId || item[parentKey] === null) && result.push(item))
  return result;
}

//将[{id: myid, name: myname, pId: mypid }] 转为树形结构 [{value: myid, label: myname, children: [] }]
export function convertToCascader(data, parentKey, firstLevelId = '0') {
  // 删除 所有 children,以防止多次调用
  data.forEach(item => {
    item.value = item.id;
    item.name = item.name;
    delete item.children;
  });
  // 将数据存储为 以Id为 KEY 的 map 索引数据列
  let map = {};
  data.forEach(item => {
    map[item.id] = item;
  });
  let list = [], result = [];
  data.forEach(item => {
    // 以当前遍历项的parentCategoryId,去map对象中找到索引的id
    let parent = map[item[parentKey]];
    // 如果找到索引，那么说明此项不在顶级当中,那么需要把此项添加到，他对应的父级中
    if (parent) {
      (parent.children || (parent.children = [])).push(item);
    } else {
      //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到list中，作为顶级
      list.push(item);
    }
  });
  list.forEach(item => (item[parentKey] === firstLevelId || item[parentKey] === null) && result.push(item))
  return result;
}

//生成条形码
export function generateBarcode(str) {
  let barcode = document.createElement('img'),
    options = {
      fontSize: 18,
      height: 100,
      displayValue: true
    };
  JsBarcode(barcode, str, options);
  return barcode.src
}
//生成二维码
export function generateQRCode(str) {
  let result = ''
  QRCode.toDataURL(str, {errorCorrectionLevel: 'H'}, function (err, url) {
    result = url;
  })
  return result
}

//使用存储在本地的pagesize
export function updatePageSize(path, state) {
  const storedPageSize = getLocalData(path);
  storedPageSize && (state.pageConfig.pageSize = storedPageSize)

  return state
}

//树结构数据转换
export function treeDataConvertor(tree, fieldName) {
  function convert(data) {
    isArray(data) && (data = data.map((item) => {
      item = {
        ...item[fieldName],
        ...item
      }
      delete item[fieldName]
      item.children = convert(item.children);
      return item;
    }));
    return data
  }
  let copyTree = cloneDeep(tree);
  copyTree = convert(copyTree);
  return copyTree;
}

export const reg={
  price:/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/,
}

//地址结构数据转换 1 为1层 2为2层
export function makeAddress(item,type,all=true){
  if(type ==1){
    for(var i=0;i<item.length;i++){
        delete item[i].children
    }
  }else if(type == 2){
    for(var i=0;i<item.length;i++){
      if(item[i].children){
        for(var j=0;j<item[i].children.length;j++){
          delete item[i].children[j].children
        }
      }
    }
    if(all){
      item.unshift({
        label: "全部",
        value: null,
      })
    }
   
  }
  return item
}

//下载文件
export function download(param) {
  // 文件类型是否为pdf
  let type = param.slice(-3);
  if (type === 'pdf') {
    var saveAs = function (blob, filename) {
      var URL = window.URL || window.webkitURL;
      var type = blob.type;
      var force_saveable_type = 'application/octet-stream';
      if (type && type != force_saveable_type) { // 强制下载，而非在浏览器中打开
        var slice = blob.slice || blob.webkitSlice || blob.mozSlice;
        blob = slice.call(blob, 0, blob.size, force_saveable_type);
      }
      var url = URL.createObjectURL(blob);
      var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
      save_link.href = url;
      save_link.download = filename;
      
      var event = new MouseEvent("click", {
        bubbles: true,
        cancelable: true,
        view: window
      });
      save_link.dispatchEvent(event);
      URL.revokeObjectURL(url);
    }
    var oReq = new XMLHttpRequest();
    var URLToPDF = param;
    oReq.open("GET", URLToPDF, true);
    oReq.responseType = "blob";
    oReq.onload = function () {
      var file = new Blob([oReq.response], {type: 'application/pdf'});
      saveAs(file, "我的策划案.pdf");
    };
    oReq.send();
    return
  }
  let elemIF = document.createElement('iframe');
  elemIF.src = param;
  elemIF.style.display = "none";
  console.log(elemIF)
  document.body.appendChild(elemIF);
  return elemIF
}
//身份证
export function formatIdCard(param){
  if(!!param){
    var strlen = param.length;
    if(strlen < 9){
      return strlen;
    }
    return param.replace(/^(.{4})(?:\d+)(.{4})$/,"$1********$2");
  }
}
//生成随机手机号

export function getMoble() {
  var prefixArray = new Array("130", "131", "132", "133", "135", "137", "138", "170", "187", "189");
  var i = parseInt(10 * Math.random());
  var prefix = prefixArray[i];
  for (var j = 0; j < 8; j++) {
  prefix = prefix + Math.floor(Math.random() * 10);
  }
  var x = document.getElementsByName("mobile_tel");
  for (var i = 0; i < x.length; i++) {
  var o = x[i];
  o.value = prefix;
  }
  return prefix
}
/***
 * 解决js精度问题
 *  method **
 *  add / subtract / multiply /divide
 * floatObj.add(0.1, 0.2) >> 0.3
 * floatObj.multiply(19.9, 100) >> 1990
 *
 */
export var FloatObj = (function() {
  
  function decNum(a){/*获取小数位数*/
    var r=0;
    a=a.toString();
    if(a.indexOf(".")!== -1) r=a.split(".")[1].length;
    return r;
  }
  function int(a){/*去除小数点并转成数值*/
    return parseInt(a.toString().replace(".",""));
  }
  function calc(a,b,type){//加减乘除
    var r,
      da=decNum(a),
      db=decNum(b),
      dsum=da+db,
      dmin=Math.min(da,db),
      dmax=Math.max(da,db);
    dsum+=dmax-dmin;
    dsum=Math.pow(10,dsum);
    dmax=Math.pow(10,dmax);
    a=int(a);
    b=int(b);
    if(da>db){
      b*=Math.pow(10,da-db);
    }else{
      a*=Math.pow(10,db-da);
    }
    switch(type){
      case "add":
        r=(a+b)/dmax;
        break;
      case "subtract":
        r=(a-b)/dmax;
        break;
      case "multiply":
        r=(a*b)/dsum;
        break;
      case "divide":
        r=a/b;
        break;
      case "mod":
        r=(multiply(a,dmax)%multiply(b,dmax))/dmax;
        break;
    }
    return r;
  }
  
  // 加减乘除的四个接口
  // 加
  function add(a, b, type) {
    return calc(a, b,'add')
  }
  // 减
  function subtract(a, b, type) {
    return calc(a, b,'subtract')
  }
  // 乘
  function multiply(a, b, type) {
    return calc(a, b, 'multiply')
  }
  // 除
  function divide(a, b, type) {
    return calc(a, b,'divide')
  }
  // 取余
  function mod(a, b, type) {
    return calc(a, b,'mod')
  }
  
  // exports
  return {
    add: add,
    subtract: subtract,
    multiply: multiply,
    divide: divide,
    mod: mod,
  }
})();

export function biAction(param){
  if(param.toUrl){
    window.open(param.toUrl)
  }else{
    hashHistory.push({
      pathname: `/home/${param.parentMenuUrl}/${param.url}`,
      query: {}
    });
  }
}
